package com.ruoyi.ex.domain;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.domain.entity.SysUser;
import lombok.*;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;

import javax.validation.constraints.NotNull;

/**
 * 考试报名记录对象 ex_study_reload
 *
 * @author zzx
 * @date 2023-05-06
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@ToString
public class ExStudyReload extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    private Long id;

    /**
     * 学生编号
     */
    private Long stuId;
    private SysUser user;

    /**
     * 机构编号
     */
    private Long deptId;

    /**
     * 题库编号
     */
    @Excel(name = "题库编号")
    private Long bankId;
    private String bankName;
    private List<Long> questionIds;//题目集合

    /**
     * 是否是初训
     */
    @Excel(name = "是否是初训")
    private Integer isFirst;

    /**
     * 考试日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "考试日期", width = 30, dateFormat = "yyyy-MM-dd")
    @NotNull(message = "考试日期不可为空")
    private Date exDate;

    /**
     * 考试时间范围
     */
    @Excel(name = "考试时间范围")
    @NotNull(message = "考试时间范围不可为空")
    private String exTime;
    private JSONObject exTimeObj;

    /**
     * 对题数
     */
    @Excel(name = "对题数")
    private Integer right;

    /**
     * 得分
     */
    @Excel(name = "得分")
    private Double score;

    /**
     * 答案列表
     */
    @Excel(name = "答案列表")
    private String answers;
    private Map<Long,Object> answerMap;

    //格式化已完成的题目
    private Map<Long, Boolean> questionAnswers;

    /**
     * 状态
     */
    @Excel(name = "状态")
    private Integer status;

    /**
     * 预留字段1
     */
    private String reservedOne;

    /**
     * 预留字段2
     */
    private String reservedTwo;

    public void setAnswerMap(Map<Long,Object> answerMap) {
        this.answerMap = answerMap;
        this.answers = JSONUtil.parseObj(answerMap).toString();
    }

    public void setAnswers(String answers) {
        this.answers = answers;
        this.answerMap = new HashMap<>();
        try{
            JSONUtil.parseObj(answers).forEach((k,v)->{
                this.answerMap.put(Long.valueOf(k),v);
            });
        }catch (Exception e){
            StaticLog.error(e);
        }
    }

    public void setExTime(Object exTime) {
        this.exTime = exTime+"";
        if (JSONUtil.isTypeJSONObject(this.exTime)){
            this.exTimeObj = JSONUtil.parseObj(this.exTime);
        }
    }
}
